package com.cat.model.web.action.admin;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.alonew.core.commons.page.Page;
import com.cat.model.biz.service.admin.CompanyInfoService;
import com.cat.model.common.model.common.CompanyInfoVO;
import com.cat.model.dao.common.PlmCommDAO;
import com.cat.model.dao.mapper.ModelMapper;
import com.cat.model.web.action.base.AdminAction;

public class CompanyInfoManagerAction extends AdminAction{
	/**
	 * 
	 */
	private static final long serialVersionUID = 425636316597986569L;

	private CompanyInfoVO companyInfo;
	private Long id;
		
	@Autowired
	private CompanyInfoService companyInfoService; 
	
	@Autowired
	private PlmCommDAO plmCommDAO;
	
	@Autowired
	private ModelMapper modelMapper;
	
	/**
	 * list
	 * 
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String list() {
		Map<String, Object> condition = new HashMap<String, Object>();
		
		Page page = this.fetchPageParams();
		page.setNumPerPage(20);
		
		Long totalCount = plmCommDAO.searchSize("manager.searchCompanyInfoSize", condition);
		page.setTotalCount(totalCount);
		int start = (page.getPageNum() -1) * page.getNumPerPage();
		if(start < 0){
			start = 0;
		}
		
		List<CompanyInfoVO> list = (List<CompanyInfoVO>)plmCommDAO.search("manager.searchCompanyInfo", condition, start, page.getNumPerPage());
		
		request.setAttribute("list", list);
		request.setAttribute("size", totalCount);
		return "list";
	}
	
	public String deleteSingle() {
		companyInfoService.deleteCompanyInfo(id);
		okAlert("删除成功！");
		return "list";
	}
	
	public String toAdd(){
		return "add";
	}
	
	public String add(){
		companyInfoService.createCompanyInfo(companyInfo);
		parentAlert(true, "保存成功");
		return "list";
	}
	
	public String toEdit(){
		companyInfo = modelMapper.getCompanyInfo(id);
		return "edit";
	}
	
	public String edit(){
		companyInfoService.updateCompanyInfo(companyInfo);
		parentAlert(true, "保存成功");
		return "list";
	}


	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CompanyInfoVO getCompanyInfo() {
		return companyInfo;
	}

	public void setCompanyInfo(CompanyInfoVO companyInfo) {
		this.companyInfo = companyInfo;
	}
}
